package xiegd.study.lambda;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: xieguangda
 * @Date: 2021年07月10日 11:08
 */
public class GdStudyHinzzz {

    @Test
    public void study1(){
        ///无参无返回值，Lambda体只需一条语句
        Runnable run1 = () -> {
            System.out.println("我是测试来来来");
        };
        run1.run();
    }

    @Test
    public void study2(){
        Runnable run2 = () -> System.out.println("我是测试0003");
        run2.run();
    }

    @Test
    public void study3(){
        //格式三、单个参数,无返回值
        Consumer<String> c1 = (String str) -> { System.out.println(str); };
        c1.accept("333");
    }


    @Test
    public void test3(){
        Comparator<Integer> com = (x, y) -> {
            System.out.println("函数式接口"+Integer.compare(x, y));
            return 2;
        };

        System.out.println(com.compare(2,3));
    }

    public static Integer toAdd(MyFun myFun,Integer num){
        return myFun.getValue(num);
    }

    @Test
    public void test4(){
        Integer num = GdStudyHinzzz.toAdd(x -> x+100, 10);
        System.out.println("num = " + num);
    }

    //Predicate<T> 断言型接口：

    @Test
    public void test5(){
        List<String> list = Arrays.asList("春秋", "只", "转载", "要事", "。");
        List<String> strList = filterStr(list, (s) -> s.length() >= 2);

        for (String str : strList) {
            //System.out.println(str);
        }


        //使用streamApi优化
        list.stream().filter((s) -> s.length() >= 2).collect(Collectors.toList()).forEach(System.out::println);
    }

    //需求：将满足条件的字符串，放入集合中
    public List<String> filterStr(List<String> list, Predicate<String> pre){
        List<String> strList = new ArrayList<>();

        for (String str : list) {
            if(pre.test(str)){
                strList.add(str);
            }
        }

        return strList;
    }

    //Function<T, R> 函数型接口：

    @Test
    public void test8(){
        String newStr = strHandler(" 北方风雪下  ", (str) -> str.trim());
        System.out.println(newStr);

        String subStr = strHandler("北方风雪下", (str) -> str.substring(2, 5));
        System.out.println(subStr);
    }

    //需求：用于处理字符串
    public String strHandler(String str, Function<String, String> fun){
        return fun.apply(str);
    }


    @Test
    public void test20(){
        List<Integer> numList = getNumList(10, () -> (int)(Math.random() * 100));

        for (Integer num : numList) {
            System.out.println(num);
        }
    }

    //需求：产生指定个数的整数，并放入集合中
    public List<Integer> getNumList(int num, Supplier<Integer> sup){
        List<Integer> list = new ArrayList<>();

        for (int i = 0; i < num; i++) {
            Integer n = sup.get();
            list.add(n);
        }

        return list;
    }


}
